package xyz.yuzegod.megawalls.tellraw;

public class MessagePart
{
    private static String TEXT_FORMAT;
    private static String CLICK_FORMAT;
    private static String HOVER_FORMAT;
    private static String INSERT_FORMAT;
    public String text;
    public String clickActionName;
    public String clickActionData;
    public String hoverActionName;
    public String hoverActionData;
    public String insertionData;
    
    public MessagePart() {
        this("");
    }
    
    public MessagePart(final String text) {
        this.text = text;
    }
    
    public boolean hasText() {
        return this.text != null && !this.text.isEmpty();
    }
    
    public void writeJson(final StringBuilder str) {
        str.append("{");
        str.append(String.format(MessagePart.TEXT_FORMAT, new JsonBuilder(this.text)));
        if (this.clickActionName != null) {
            str.append(",");
            str.append(String.format(MessagePart.CLICK_FORMAT, this.clickActionName, new JsonBuilder(this.clickActionData)));
        }
        if (this.hoverActionName != null) {
            str.append(",");
            str.append(String.format(MessagePart.HOVER_FORMAT, this.hoverActionName, new JsonBuilder(this.hoverActionData)));
        }
        if (this.insertionData != null) {
            str.append(",");
            str.append(String.format(MessagePart.INSERT_FORMAT, new JsonBuilder(this.insertionData)));
        }
        str.append("}");
    }
    
    static {
        MessagePart.TEXT_FORMAT = "\"text\":\"%s\"";
        MessagePart.CLICK_FORMAT = "\"clickEvent\":{\"action\":\"%s\",\"value\":\"%s\"}";
        MessagePart.HOVER_FORMAT = "\"hoverEvent\":{\"action\":\"%s\",\"value\":\"%s\"}";
        MessagePart.INSERT_FORMAT = " \"insertion\":\"%s\"";
    }
}
